package be.dioubs.sportTrack;

import be.dioubs.sportTrack.common.GlobalFormatter;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

public class SportTrack extends TabActivity {

    public static final int MENU_ITEM_DELETE = Menu.FIRST;
    public static final int MENU_ITEM_INSERT = Menu.FIRST + 1;
    
    private static final int TAB_TRACKS = 0;
    private static final int TAB_STATS = 1;
    private static final int TAB_SPORTS = 2;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		Log.d("DIOUBS","enter in onCreate");
		
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.sport_track);
	    
	    // Prepare Class to be used later
	    GlobalFormatter.setContext(SportTrack.this);

	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab
	    View tab; // Reusable View for each tab
	    
	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, TabTracks.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    tab = View.inflate(this, R.layout.tab_header_tracks, null);
	    spec = tabHost.newTabSpec("tracks").setIndicator(tab)
	    		.setContent(intent);
	    tabHost.addTab(spec);
	    tab.setOnTouchListener(onTouchListener);
	    
	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, TabStats.class);
	    tab = View.inflate(this, R.layout.tab_header_stats, null);
	    spec = tabHost.newTabSpec("tracks").setIndicator(tab)
	    		.setContent(intent);
	    tabHost.addTab(spec);
	    tab.setOnTouchListener(onTouchListener);

	    intent = new Intent().setClass(this, TabSports.class);
	    tab = View.inflate(this, R.layout.tab_header_sports, null);
	    spec = tabHost.newTabSpec("tracks").setIndicator(tab)
	    		.setContent(intent);
	    tabHost.addTab(spec);
	    tab.setOnTouchListener(onTouchListener);

	    tabHost.setCurrentTab(TAB_TRACKS);
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        
        /*default menu for all tabs*/
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, menu);
    	menu.getItem(0).getSubMenu().setHeaderIcon(R.drawable.option);
 
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.add_track:
    		Toast.makeText(SportTrack.this, "Add_track", Toast.LENGTH_LONG).show();
    		return true;
        case R.id.option:
    		Toast.makeText(SportTrack.this, "Option", Toast.LENGTH_SHORT).show();
    		return true;
        case R.id.favoris:
            Toast.makeText(SportTrack.this, "Favoris", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.stats:
            Toast.makeText(SportTrack.this, "Stats", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.quitter:
            finish();
            return true;
        }
        return true;
    }

    // Listener for Touch events
    private OnTouchListener onTouchListener = new OnTouchListener() {
    	public boolean onTouch(View view, MotionEvent motion) {
			switch(motion.getAction()) {
				case MotionEvent.ACTION_DOWN:
					touchDown(view);
					break;
				case MotionEvent.ACTION_UP:
					touchUp(view);
					break;
				case MotionEvent.ACTION_OUTSIDE:
					Toast.makeText(SportTrack.this, "Outside", Toast.LENGTH_SHORT).show();
					break;
				case MotionEvent.ACTION_CANCEL:
					Toast.makeText(SportTrack.this, "Cancel", Toast.LENGTH_SHORT).show();
					break;
			}

			return true;
    	}
    };
    
    // When user touch the screen (before releasing his finger)
    private void touchDown(View view) {
    	Drawable clicked = getResources().getDrawable(R.drawable.tab_clicked);
    	view.setBackgroundDrawable(clicked);
    }    
    
    // When user releases his finger from the screen
    private void touchUp(View view) {
    	TabHost tabHost = getTabHost();
    	TabWidget tabWidget = tabHost.getTabWidget();
    	View tracks = tabWidget.getChildAt(TAB_TRACKS);
    	View stats = tabWidget.getChildAt(TAB_STATS);
    	View sports = tabWidget.getChildAt(TAB_SPORTS);
		Drawable backgroundSelected = getResources().getDrawable(R.drawable.tab_selected);
		Drawable backgroundUnselected = getResources().getDrawable(R.drawable.tab_unselected);
		Drawable imageTracksWhite = getResources().getDrawable(R.drawable.tracks_white);
		Drawable imageTracksGrey = getResources().getDrawable(R.drawable.tracks_grey);
		Drawable imageStatsWhite = getResources().getDrawable(R.drawable.stats_white);
		Drawable imageStatsGrey = getResources().getDrawable(R.drawable.stats_grey);
		Drawable imageSportsWhite = getResources().getDrawable(R.drawable.sports_white);
		Drawable imageSportsGrey = getResources().getDrawable(R.drawable.sports_grey);
		
		// Did not manage to use R.color
		String textColorSelected = "#333333";
		String textColorUnSelected = "#777777";
		
		switch(view.getId()) {
			case R.id.tracks:
				changeDrawables(tracks, backgroundSelected, imageTracksWhite, textColorSelected);
				changeDrawables(stats, backgroundUnselected, imageStatsGrey, textColorUnSelected);
				changeDrawables(sports, backgroundUnselected, imageSportsGrey, textColorUnSelected);
				
				tabHost.setCurrentTab(TAB_TRACKS);
				
				break;
			case R.id.stats:
				changeDrawables(tracks, backgroundUnselected, imageTracksGrey, textColorUnSelected);
				changeDrawables(stats, backgroundSelected, imageStatsWhite, textColorSelected);
				changeDrawables(sports, backgroundUnselected, imageSportsGrey, textColorUnSelected);
				
				tabHost.setCurrentTab(TAB_STATS);
				
				break;
			case R.id.sports:
				changeDrawables(tracks, backgroundUnselected, imageTracksGrey, textColorUnSelected);
				changeDrawables(stats, backgroundUnselected, imageStatsGrey, textColorUnSelected);
				changeDrawables(sports, backgroundSelected, imageSportsWhite, textColorSelected);
				
				tabHost.setCurrentTab(TAB_SPORTS);
				
				break;
		}
    }  
    
    // Helps change the background image,the icon & the text of a View (Tab) 
    private void changeDrawables(View view, Drawable background, Drawable image, String color) {
    	ImageView imageView = (ImageView) view.findViewById(R.id.tab_image);
    	TextView textView = (TextView) view.findViewById(R.id.tab_title);
    	
    	view.setBackgroundDrawable(background);
    	imageView.setImageDrawable(image);
    	textView.setTextColor(Color.parseColor(color));
    }
}